home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SNNSV32.ZIP / SNNSv3.2 / kernel / sources / cc_rcc.c < prev    next >
C/C++ Source or Header  |  1994-04-25  |  27KB  |  977 lines

  1. /*****************************************************************************
  2.   FILE           : cc_rcc.c
  3.   SHORTNAME      : 
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : Common functions of CC and RCC
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Michael Schmalzl
  10.   DATE           : 5.2.93
  11.  
  12.   CHANGED BY     : Michael Vogt, Guenter Mamier
  13.   IDENTIFICATION : @(#)cc_rcc.c    1.12 4/12/94
  14.   SCCS VERSION   : 1.12
  15.   LAST CHANGE    : 4/12/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <time.h>  
  24. #include <memory.h>
  25. #include <malloc.h>
  26. #include <values.h>
  27.  
  28. #include "kr_typ.h"    
  29. #include "kr_const.h"     
  30. #include "kr_def.h"     
  31. #include "random.h"
  32. #include "kr_mac.h"
  33. #include "kernel.h"
  34. #include "kr_ui.h"
  35. #include "kr_newpattern.h"
  36. #include "cc_mac.h"    
  37. #include "cc_type.h"
  38. #include "cc_rcc.ph"   
  39.  
  40. /*****************************************************************************
  41.   FUNCTION : cc_freeStorage
  42.  
  43.   PURPOSE  : Frees all the storage allocated by CC or RCC.
  44.   NOTES    :
  45.  
  46.   UPDATE   : 5.2.93
  47. ******************************************************************************/
  48.  
  49.  
  50. krui_err cc_freeStorage(int dummy1, int dummy2, int flag)
  51. {
  52.  int p,s;
  53.  struct Unit *unitPtr;
  54.  struct Link *linkPtr;
  55.  int start, end;
  56.  
  57.  start = old.StartPattern;
  58.  end = old.EndPattern;
  59.  
  60.  cc_storageFree = 1;
  61.  
  62.  if(flag==1){
  63.    (void) cc_deleteAllSpecialAndAllHiddenUnits();
  64.    cc_end = 0;
  65.  
  66.    FOR_ALL_UNITS(unitPtr){
  67.      if(UNIT_IN_USE(unitPtr) && IS_OUTPUT_UNIT(unitPtr)){
  68.        unitPtr->value_a =  unitPtr->value_b = unitPtr->value_c = 0;
  69.        FOR_ALL_LINKS(unitPtr,linkPtr){
  70.          linkPtr->value_a =  linkPtr->value_b = linkPtr->value_c = 0;
  71.        }
  72.      }
  73.    }
  74.  }
  75.  
  76.  IF_PTR_IS_NOT_NULL(OutputUnitError) {
  77.    FOR_ALL_PATTERNS(start,end,p) 
  78.      IF_PTR_IS_NOT_NULL(OutputUnitError[p])
  79.        free(OutputUnitError[p]);
  80.    free(OutputUnitError);
  81.    OutputUnitError = NULL;
  82.  }
  83.  
  84.  IF_PTR_IS_NOT_NULL(SpecialUnitAct) {
  85.    FOR_ALL_PATTERNS(start,end,p) 
  86.      IF_PTR_IS_NOT_NULL(SpecialUnitAct[p])
  87.        free(SpecialUnitAct[p]);
  88.    free(SpecialUnitAct);
  89.    SpecialUnitAct = NULL;
  90.  }
  91.  
  92.  IF_PTR_IS_NOT_NULL(CorBetweenSpecialActAndOutError) {
  93.      for(s=0;s<OldNoOfSpecialUnitStorage;s++){
  94.        IF_PTR_IS_NOT_NULL(CorBetweenSpecialActAndOutError[s])
  95.          free(CorBetweenSpecialActAndOutError[s]); 
  96.      }
  97.    free(CorBetweenSpecialActAndOutError);  
  98.    CorBetweenSpecialActAndOutError = NULL;
  99.  } 
  100.  
  101.  IF_PTR_IS_NOT_NULL(OutputUnitSumError) {
  102.    free(OutputUnitSumError);
  103.    OutputUnitSumError = NULL;
  104.  }
  105.  
  106.  IF_PTR_IS_NOT_NULL(SpecialUnitSumAct) {
  107.    free(SpecialUnitSumAct);
  108.    SpecialUnitSumAct = NULL;
  109.  }
  110.  
  111.  return(KRERR_NO_ERROR);
  112. }
  113.  
  114. /*****************************************************************************
  115.   FUNCTION : cc_allocateStorage
  116.  
  117.   PURPOSE  : Allocates all the storage needed by CC and RCC
  118.   NOTES    :
  119.  
  120.   UPDATE   : 5.2.93
  121. ******************************************************************************/
  122.  
  123. krui_err cc_allocateStorage(int StartPattern, int  EndPattern, int NoOfSpecialUnits)
  124. {
  125.  int s,p;
  126.  struct Unit *dummyPtr;
  127.  int start, end;
  128.  
  129.  OldNoOfSpecialUnitStorage = NoOfSpecialUnits;
  130.  cc_storageFree = 0;
  131.  
  132.  KernelErrorCode = kr_initSubPatternOrder(StartPattern,EndPattern);
  133.  ERROR_CHECK;
  134.  start = kr_AbsPosOfFirstSubPat(StartPattern);
  135.  end   = kr_AbsPosOfFirstSubPat(EndPattern);
  136.  end  += kr_NoOfSubPatPairs(EndPattern) - 1;
  137.  
  138.  OutputUnitSumError = (float *) calloc(NoOfOutputUnits,sizeof(float));
  139.  if(OutputUnitSumError == NULL) {
  140.    CC_ERROR(KRERR_CC_ERROR3);
  141.  }
  142.  SpecialUnitSumAct = (float *) calloc(NoOfSpecialUnits,sizeof(float));
  143.  if(SpecialUnitSumAct == NULL) {
  144.    CC_ERROR(KRERR_CC_ERROR3);
  145.  }
  146.  OutputUnitError = (float **)calloc(end-start+1,sizeof(float *));
  147.  if(OutputUnitError == NULL) {
  148.    CC_ERROR(KRERR_CC_ERROR3);
  149.  }
  150.  SpecialUnitAct = (float **)calloc(end-start+1,sizeof(float *));
  151.  if(SpecialUnitAct == NULL) {
  152.    CC_ERROR(KRERR_CC_ERROR3);
  153.  }
  154.  CorBetweenSpecialActAndOutError =  (float **)calloc(NoOfSpecialUnits,sizeof(float *));
  155.  if(CorBetweenSpecialActAndOutError == NULL) {
  156.    CC_ERROR(KRERR_CC_ERROR3);
  157.  }
  158.  
  159.  
  160.  FOR_ALL_PATTERNS(start,end,p) {
  161.    OutputUnitError[p] = (float *)calloc(NoOfOutputUnits,sizeof(float));
  162.    if(OutputUnitError[p] == NULL) {
  163.      CC_ERROR(KRERR_CC_ERROR3);
  164.    }
  165.    SpecialUnitAct[p] = (float *)calloc(NoOfSpecialUnits,sizeof(float));
  166.    if(SpecialUnitAct[p] == NULL) {
  167.      CC_ERROR(KRERR_CC_ERROR3);
  168.    }
  169.  }
  170.  
  171.  FOR_ALL_SPECIAL_UNITS(dummyPtr,s) {
  172.    CorBetweenSpecialActAndOutError[s] = (float *)calloc(NoOfOutputUnits,sizeof(float));   
  173.    if(CorBetweenSpecialActAndOutError[s] == NULL) {
  174.      CC_ERROR(KRERR_CC_ERROR3);
  175.    }
  176.  } 
  177.  
  178.  old.EndPattern       = end;
  179.  old.StartPattern     = start;
  180.  
  181.  return(KRERR_NO_ERROR);
  182. }
  183.  
  184. /*****************************************************************************
  185.   FUNCTION : cc_deleteAllSpecialAndAllHiddenUnits
  186.  
  187.   PURPOSE  : Deletes all special units and all hidden units.
  188.   NOTES    :
  189.  
  190.   UPDATE   : 5.2.93
  191. ******************************************************************************/
  192.  
  193. krui_err cc_deleteAllSpecialAndAllHiddenUnits(void)
  194. {
  195.  struct Unit *UnitPtr;
  196.  
  197.  if(NoOfUnits != 0) {
  198.    FOR_ALL_UNITS(UnitPtr){
  199.      if((IS_HIDDEN_UNIT(UnitPtr) || IS_SPECIAL_UNIT(UnitPtr)) && UNIT_IN_USE(UnitPtr)) {
  200.        KernelErrorCode = kr_removeUnit(UnitPtr); 
  201.        ERROR_CHECK;
  202.      }
  203.    }
  204.    kr_forceUnitGC();
  205.    NoOfHiddenUnits = 0;
  206.    NetModified = 1;
  207.  }
  208.  return(KRERR_NO_ERROR);
  209.  
  210. /*****************************************************************************
  211.   FUNCTION : rcc_manageResetArray
  212.  
  213.   PURPOSE  : Allocates the storage of the reset array in needed.
  214.   NOTES    :
  215.  
  216.   UPDATE   : 5.2.93
  217. ******************************************************************************/
  218.  
  219. void rcc_manageResetArray(int startPattern, int endPattern, int flag) 
  220. {
  221.  int p;
  222.  int start, end;
  223.  int pat, sub;
  224.  int size;
  225.  Patterns pat_ptr;
  226.  
  227.  if(reset!=NULL){
  228.    free(reset);
  229.  }
  230.  reset = NULL;
  231.  
  232.  if(flag){
  233.    reset = (int *) calloc(kr_TotalNoOfSubPatPairs(),sizeof(int));
  234.    reset[0] = 1;
  235.    KernelErrorCode = kr_initSubPatternOrder(startPattern,endPattern);
  236.    if (KernelErrorCode != KRERR_NO_ERROR)
  237.        return;
  238.    start = kr_AbsPosOfFirstSubPat(startPattern+1);
  239.    end   = kr_AbsPosOfFirstSubPat(endPattern-1);
  240.    end  += kr_NoOfSubPatPairs(endPattern-1) - 1;
  241.    for(p=start; p<=end;p++){
  242.        kr_getSubPatternByNo(&pat,&sub,p);
  243.        pat_ptr = kr_getSubPatData(pat,sub,OUTPUT,&size);
  244.        if( *(pat_ptr + size - 1) > 0.0 ) {
  245.        reset[p+1] = 1; 
  246.      }
  247.    }
  248.  }
  249. }
  250.  
  251. /*****************************************************************************
  252.   FUNCTION : rcc_manageLinkArray
  253.  
  254.   PURPOSE  : Allocates the storage of the link array if needed.
  255.   NOTES    :
  256.  
  257.   UPDATE   : 5.2.93
  258. ******************************************************************************/
  259.  
  260. void rcc_manageLinkArray(int newNoOfSpecialUnits, int flag)
  261. {
  262.  static int oldNoOfSpecialUnits=0;
  263.  int i,noOfHiddenUnits=0;
  264.  struct Unit *unit_ptr;
  265.  
  266.  for(i=0;i<oldNoOfSpecialUnits;i++){
  267.    if(linkArray[i]!=NULL){
  268.      free(linkArray[i]);
  269.    }
  270.  }
  271.  if(linkArray!=NULL){
  272.    free(linkArray);
  273.  }
  274.  
  275.  oldNoOfSpecialUnits = 0;
  276.  
  277.  if(flag){
  278.    FOR_ALL_UNITS(unit_ptr){
  279.      if(IS_HIDDEN_UNIT(unit_ptr)){
  280.        noOfHiddenUnits++;
  281.      }
  282.    }
  283.    linkArray = (float **) calloc(newNoOfSpecialUnits,sizeof(float *));
  284.  
  285.    for(i=0;i<newNoOfSpecialUnits;i++){
  286.      linkArray[i] = (float *) calloc(newNoOfSpecialUnits+noOfHiddenUnits+2,sizeof(float));
  287.    }
  288.    
  289.    oldNoOfSpecialUnits = newNoOfSpecialUnits;
  290.  }
  291. }
  292.  
  293.  
  294. /*****************************************************************************
  295.   FUNCTION : cc_initErrorArrays
  296.  
  297.   PURPOSE  : 
  298.   NOTES    :
  299.  
  300.   UPDATE   : 5.2.93
  301. ******************************************************************************/
  302.  
  303. void cc_initErrorArrays(void)
  304. {
  305.  int o;
  306.  struct Unit *outputUnitPtr;
  307.  
  308.   FOR_ALL_OUTPUT_UNITS(outputUnitPtr,o) {
  309.     OutputUnitSumError[o] = 0.0;
  310.   }
  311. }
  312.  
  313. /*****************************************************************************
  314.   FUNCTION : cc_initActivationArrays
  315.  
  316.   PURPOSE  : 
  317.   NOTES    :
  318.  
  319.   UPDATE   : 5.2.93
  320. ******************************************************************************/
  321.  
  322. void cc_initActivationArrays(void)
  323. {
  324.  int s,o;
  325.  struct Unit *outputUnitPtr,*specialUnitPtr;
  326.  
  327.   FOR_ALL_SPECIAL_UNITS(specialUnitPtr,s) {
  328.     SpecialUnitSumAct[s] = 0.0;
  329.   }
  330.  
  331.  FOR_ALL_SPECIAL_UNITS(specialUnitPtr,s) {
  332.    FOR_ALL_OUTPUT_UNITS(outputUnitPtr,o) {
  333.     CorBetweenSpecialActAndOutError[s][o] = 0.0;
  334.   }
  335.  } 
  336. }
  337.  
  338.  
  339. /*****************************************************************************
  340.   FUNCTION : cc_generateRandomNo
  341.  
  342.   PURPOSE  : Generates a random number in the interval [-maxValue,+maxValue]
  343.   NOTES    :
  344.  
  345.   UPDATE   : 5.2.93
  346. ******************************************************************************/
  347.  
  348. FlintType cc_generateRandomNo(float maxValue)
  349. {
  350.  return (FlintType)(drand48()*2.0*maxValue-maxValue);  
  351. }
  352.  
  353. /*****************************************************************************
  354.   FUNCTION : cc_compareActFunctions
  355.  
  356.   PURPOSE  : Tests wether the selected activation functions are consistent. 
  357.   NOTES    :
  358.  
  359.   UPDATE   : 5.2.93
  360. ******************************************************************************/
  361.  
  362.  
  363. void cc_compareActFunctions(int actFunc, int flag)
  364. {
  365.  int found_h=0,found_o=0;
  366.  struct Unit *unit_ptr,*hiddenUnitPtr,*outputUnitPtr;
  367.  char name_o[30],name_h[30];
  368.  
  369.  if((actFunc != RANDOM) && cc_printOnOff){
  370.    FOR_ALL_UNITS(unit_ptr){
  371.      if(IS_HIDDEN_UNIT(unit_ptr) && found_h != 1){
  372.        hiddenUnitPtr = unit_ptr;
  373.        strcpy(name_h,krui_getUnitActFuncName(GET_UNIT_NO(outputUnitPtr)));
  374.        found_h = 1;
  375.      }
  376.      if(IS_OUTPUT_UNIT(unit_ptr) && found_o != 1){
  377.        outputUnitPtr = unit_ptr;
  378.        strcpy(name_o,krui_getUnitActFuncName(GET_UNIT_NO(outputUnitPtr)));
  379.        found_o = 1;
  380.      }
  381.      if(found_h && found_o){
  382.        break;
  383.      }
  384.    }
  385.    if(flag == CC){
  386.      if(found_o && strcmp(cc_actFuncArray[actFunc],name_o)){
  387.        printf("!!! Warning: Output units and special units have different act. functions !!!\n");
  388.      }
  389.    }
  390.    else {
  391.      if(found_o && strcmp(rcc_actFuncArray[actFunc],name_o)){
  392.        printf("!!! Warning: Output units and special units have different act. functions !!! \n");
  393.      }
  394.    }
  395.    if(flag == CC){
  396.      if(found_h && strcmp(cc_actFuncArray[actFunc],name_h)){
  397.        printf("!!! Warning: Hidden units and special units have different act. functions !!!\n");
  398.      }
  399.    }
  400.    else {
  401.      if(found_h && strcmp(rcc_actFuncArray[actFunc],name_h)){
  402.        printf("!!! Warning: Hidden units and special units have different act. functions !!! \n");
  403.      }
  404.    }
  405.  
  406.    if(found_h && (outputUnitPtr->act_func != hiddenUnitPtr->act_func)){
  407.      printf("!!! Warning:  Output units and hidden units have different act. functions !!! \n");
  408.    }
  409.  }
  410. }
  411.  
  412. /*****************************************************************************
  413.   FUNCTION : cc_changeActFuncOfSpecialUnit
  414.  
  415.   PURPOSE  : Changes the activation function of the special units.
  416.   NOTES    :
  417.  
  418.   UPDATE   : 5.2.93
  419. ******************************************************************************/
  420.  
  421. krui_err cc_changeActFuncOfSpecialUnits(int type, int LearnFunc)
  422. {
  423.  int s,selector;
  424.  struct Unit *specialUnitPtr;
  425.  
  426.  FOR_ALL_SPECIAL_UNITS(specialUnitPtr,s) {
  427.    if(type==RANDOM){
  428.      selector = s % (NO_OF_ACT_FUNCS - 1);
  429.    }
  430.    else {
  431.      selector = type;
  432.    }
  433.    if(LearnFunc == CC){
  434.      KernelErrorCode = krui_setUnitActFunc(GET_UNIT_NO(specialUnitPtr),cc_actFuncArray[selector]);
  435.      ERROR_CHECK;  
  436.    }
  437.    else {
  438.      KernelErrorCode = krui_setUnitActFunc(GET_UNIT_NO(specialUnitPtr),cc_actFuncArray[selector]);
  439.      ERROR_CHECK;  
  440.    }
  441.  }
  442.  return(KRERR_NO_ERROR);
  443. }
  444.  
  445. /*****************************************************************************
  446.   FUNCTION : cc_calculateNetParameters
  447.  
  448.   PURPOSE  : Calculates the position of the current input, hidden and output
  449.              layer.
  450.   NOTES    :
  451.  
  452.   UPDATE   : 5.2.93
  453. ******************************************************************************/
  454.  
  455. krui_err cc_calculateNetParameters(int *maxYPosOfHiddenUnit, int *xPosOfLastInsertedHiddenUnit,
  456.                                    int *yPosOfLastInsertedHiddenUnit, int *outXMax)
  457. {         
  458.  struct Unit *unitPtr;
  459.  int x,y;
  460.  int inputXMin=0,inputXMax=0,hiddenXMin=0,hiddenXMax=0,outputXMin=0,outputXMax=0;
  461.  int inputYMin=0,inputYMax=0,hiddenYMin=0,hiddenYMax=0,outputYMin=0,outputYMax=0;
  462.  int xInputOffset=0,xHiddenOffset=0,xOutputOffset=0;
  463.  int yInputOffset=0,yHiddenOffset=0,yOutputOffset=0;
  464.  
  465.  NoOfInputUnits  = 0;
  466.  NoOfHiddenUnits = 0;
  467.  NoOfOutputUnits = 0;
  468.   
  469.  FOR_ALL_UNITS(unitPtr){
  470.    if(IS_INPUT_UNIT(unitPtr) && UNIT_IN_USE(unitPtr)){
  471.      x = GET_UNIT_XPOS(unitPtr);
  472.      y = GET_UNIT_YPOS(unitPtr);
  473.      NoOfInputUnits++;
  474.      if((y >= inputYMax) || (inputYMax==0)){
  475.        inputYMax = y;
  476.      }
  477.      if((y <= inputYMin) || (inputYMin==0)){
  478.        inputYMin = y;
  479.      }
  480.      if((x >= inputXMax) || (inputXMax==0)){
  481.        inputXMax = x;
  482.      }
  483.      if((x <= inputXMin) || (inputXMin==0)){
  484.        inputXMin = x;
  485.      }
  486.    }
  487.    else if(IS_HIDDEN_UNIT(unitPtr) && UNIT_IN_USE(unitPtr)){
  488.      x = GET_UNIT_XPOS(unitPtr);
  489.      y = GET_UNIT_YPOS(unitPtr);
  490.      NoOfHiddenUnits++;
  491.      if((y >= hiddenYMax) || (hiddenYMax==0)){
  492.        hiddenYMax = y;
  493.      }
  494.      if((y <= hiddenYMin) || (hiddenYMin==0)){
  495.        hiddenYMin = y;
  496.      }
  497.      if((x >= hiddenXMax) || (hiddenXMax==0)){
  498.        hiddenXMax = x;
  499.      }
  500.      if((x <= hiddenXMin) || (hiddenXMin==0)){
  501.        hiddenXMin = x;
  502.      }
  503.    }
  504.    else if(IS_OUTPUT_UNIT(unitPtr) && UNIT_IN_USE(unitPtr)){
  505.      x = GET_UNIT_XPOS(unitPtr);
  506.      y = GET_UNIT_YPOS(unitPtr);
  507.      NoOfOutputUnits++;
  508.      if((y >= outputYMax) || (outputYMax==0)){
  509.        outputYMax = y;
  510.      }
  511.      if((y <= outputYMin) || (outputYMin==0)){
  512.        outputYMin = y;
  513.      }
  514.      if((x >= outputXMax) || (outputXMax==0)){
  515.        outputXMax = x;
  516.      }
  517.      if((x <= outputXMin) || (outputXMin==0)){
  518.        outputXMin = x;
  519.      }
  520.    }
  521.  }
  522.  
  523.  if(NoOfHiddenUnits==0){
  524.    hiddenXMax = hiddenXMin = X_MIN_POS + (inputXMax-inputXMin) + 3;
  525.    hiddenYMax = hiddenYMin = Y_MIN_POS;
  526.    *outXMax = X_MIN_POS + (inputXMax-inputXMin) + (hiddenXMax-hiddenXMin) + 6;
  527.    *xPosOfLastInsertedHiddenUnit = X_MIN_POS + (inputXMax - inputXMin) + 3;
  528.  }
  529.  else {
  530.    *xPosOfLastInsertedHiddenUnit = X_MIN_POS + (inputXMax - inputXMin) + (hiddenXMax - hiddenXMin) + 3;
  531.    *outXMax = X_MIN_POS + (inputXMax-inputXMin) + (hiddenXMax-hiddenXMin) + (outputXMax-outputXMin) + 6;
  532.  }
  533.  *yPosOfLastInsertedHiddenUnit = Y_MIN_POS-1;
  534.  if(NoOfHiddenUnits==0) {
  535.    *maxYPosOfHiddenUnit = Y_MIN_POS + HIDDEN_LAYER_HEIGHT - 1;
  536.  }
  537.  else {
  538.    if(hiddenYMax - hiddenYMin + 1 >= MIN_HIDDEN_LAYER_HEIGHT) {
  539.      *maxYPosOfHiddenUnit = Y_MIN_POS + (hiddenYMax-hiddenYMin);
  540.    }
  541.    else {
  542.      *maxYPosOfHiddenUnit = Y_MIN_POS + HIDDEN_LAYER_HEIGHT - 1;
  543.    }
  544.  }
  545.  
  546.  xInputOffset = X_MIN_POS - inputXMin;
  547.  yInputOffset = Y_MIN_POS - inputYMin;
  548.  
  549.  xHiddenOffset = X_MIN_POS + (inputXMax-inputXMin) - hiddenXMin + 3;
  550.  yHiddenOffset = Y_MIN_POS - hiddenYMin;
  551.  
  552.  xOutputOffset = X_MIN_POS + (inputXMax-inputXMin) + (hiddenXMax-hiddenXMin) - outputXMin + 6;
  553.  yOutputOffset = Y_MIN_POS - outputYMin;   
  554.  
  555.  FOR_ALL_UNITS(unitPtr){
  556.    if(IS_INPUT_UNIT(unitPtr) && UNIT_IN_USE(unitPtr) ) {
  557.      SET_UNIT_XPOS(unitPtr,GET_UNIT_XPOS(unitPtr)+xInputOffset);
  558.      SET_UNIT_YPOS(unitPtr,GET_UNIT_YPOS(unitPtr)+yInputOffset);
  559.    }
  560.    if(IS_HIDDEN_UNIT(unitPtr) && UNIT_IN_USE(unitPtr)) {
  561.      SET_UNIT_XPOS(unitPtr,GET_UNIT_XPOS(unitPtr)+xHiddenOffset);
  562.      SET_UNIT_YPOS(unitPtr,GET_UNIT_YPOS(unitPtr)+yHiddenOffset);
  563.    }
  564.    if(IS_OUTPUT_UNIT(unitPtr) && UNIT_IN_USE(unitPtr) ) {
  565.      SET_UNIT_XPOS(unitPtr,GET_UNIT_XPOS(unitPtr)+xOutputOffset);
  566.      SET_UNIT_YPOS(unitPtr,GET_UNIT_YPOS(unitPtr)+yOutputOffset);
  567.    }
  568.  }
  569.  
  570.  if(NoOfHiddenUnits!=0) {
  571.    FOR_ALL_UNITS(unitPtr){
  572.      if(IS_HIDDEN_UNIT(unitPtr) && UNIT_IN_USE(unitPtr)) {
  573.        x = GET_UNIT_XPOS(unitPtr);
  574.        y = GET_UNIT_YPOS(unitPtr);
  575.        if(x == *xPosOfLastInsertedHiddenUnit){
  576.          if(y >= *yPosOfLastInsertedHiddenUnit){
  577.            *yPosOfLastInsertedHiddenUnit = y;
  578.          }
  579.        }
  580.      }
  581.    }
  582.  }
  583.  return(KRERR_NO_ERROR);
  584. }
  585.  
  586.   
  587. /*****************************************************************************
  588.   FUNCTION : cc_initOutputUnits
  589.  
  590.   PURPOSE  : Initializes the links of the output units.
  591.   NOTES    :
  592.  
  593.   UPDATE   : 5.2.93
  594. ******************************************************************************/
  595.  
  596. void cc_initOutputUnits(void)
  597. {
  598.  struct Unit *outputUnitPtr;
  599.  struct Link *linkPtr;
  600.  int o;
  601.  
  602.  FOR_ALL_OUTPUT_UNITS(outputUnitPtr,o){
  603.    outputUnitPtr->value_a =  outputUnitPtr->value_b = outputUnitPtr->value_c = 0;
  604.    FOR_ALL_LINKS(outputUnitPtr,linkPtr){
  605.      linkPtr->value_a =  linkPtr->value_b = linkPtr->value_c = 0;
  606.    }
  607.  }
  608. }
  609.  
  610.  
  611. /*****************************************************************************
  612.   FUNCTION : cc_calculateCorrelation
  613.  
  614.   PURPOSE  : Calculates the correlation of the pool of candidate units and 
  615.              returns the high score.
  616.   NOTES    :
  617.  
  618.   UPDATE   : 5.2.93
  619. ******************************************************************************/
  620.  
  621. float cc_calculateCorrelation(int StartPattern, int EndPattern, int counter)
  622. {
  623.  int s,o,n;
  624.  float highScore=0.0,bestSpecialUnitScore,scoreBuffer;
  625.  struct Unit *SpecialUnitPtr,*OutputUnitPtr;
  626.  int start, end;
  627.  
  628.  KernelErrorCode = kr_initSubPatternOrder(StartPattern,EndPattern);
  629.  ERROR_CHECK;
  630.  start = kr_AbsPosOfFirstSubPat(StartPattern+1);
  631.  end   = kr_AbsPosOfFirstSubPat(EndPattern-1);
  632.  end  += kr_NoOfSubPatPairs(EndPattern-1) - 1;
  633.  
  634.  n = end - start + 1;
  635.  
  636.  bestSpecialUnitPtr = NULL;
  637.  bestSpecialUnitScore = 0.0;
  638.  if(cc_printOnOff) {
  639.    printf("Cycle %d ",counter);
  640.  } 
  641.  
  642.  
  643.  FOR_ALL_SPECIAL_UNITS(SpecialUnitPtr,s) {
  644.    FOR_ALL_OUTPUT_UNITS(OutputUnitPtr,o) {
  645.      scoreBuffer =  (CorBetweenSpecialActAndOutError[s][o] - 
  646.                     (OutputUnitSumError[o]/n  * SpecialUnitSumAct[s])) / SumSqError;
  647.      highScore += fabs(scoreBuffer);
  648.      CorBetweenSpecialActAndOutError[s][o] = SIGN(scoreBuffer);
  649.    }
  650.    if(highScore > bestSpecialUnitScore) {
  651.      bestSpecialUnitScore = highScore;
  652.      bestSpecialUnitPtr = SpecialUnitPtr;
  653.    }
  654.    if(cc_printOnOff) {
  655.      printf("S[%d]: %.4f ",s,highScore);
  656.    }
  657.    highScore = 0.0;
  658.  }
  659.  if(cc_printOnOff) {
  660.    printf("\n");
  661.  } 
  662.  
  663.  return(bestSpecialUnitScore);
  664. }
  665.  
  666. /*****************************************************************************
  667.   FUNCTION : cc_initInputUnitsWithPattern
  668.  
  669.   PURPOSE  : Initialize the input layer with pattern PatternNo.
  670.   NOTES    :
  671.  
  672.   UPDATE   : 5.2.93
  673. ******************************************************************************/
  674.  
  675. void cc_initInputUnitsWithPattern(int SubPatternNo)
  676. {
  677.   register struct Unit   *unit_ptr;
  678.   register Patterns  in_pat;
  679.   register int dummy;
  680.   int pat, sub;
  681.   
  682.  
  683.   kr_getSubPatternByNo(&pat,&sub,SubPatternNo);
  684.   in_pat = kr_getSubPatData(pat,sub,INPUT,NULL);
  685.  
  686.   FOR_ALL_INPUT_UNITS(unit_ptr,dummy){
  687.     if(unit_ptr->out_func == OUT_IDENTITY) {
  688.       unit_ptr->Out.output = unit_ptr->act = *in_pat++;
  689.     }
  690.     else {
  691.       unit_ptr->Out.output = (*unit_ptr->out_func) (unit_ptr->act = *in_pat++);
  692.     }
  693.   }
  694. }
  695.  
  696.  
  697.  
  698. /*****************************************************************************
  699.   FUNCTION : cc_setHiddenUnit
  700.  
  701.   PURPOSE  : Positions the hidden layer.
  702.   NOTES    :
  703.  
  704.   UPDATE   : 5.2.93
  705. ******************************************************************************/
  706.  
  707. krui_err cc_setHiddenUnit(struct Unit *hiddenUnitPtr, int maxYPosOfHiddenUnit,
  708.                           int *xPosOfLastInsertedHiddenUnit, int *yPosOfLastInsertedHiddenUnit)
  709.  
  710. {
  711.  struct Unit *outputUnitPtr,*specialUnitPtr;
  712.  int dummy;
  713.  
  714.  if(*yPosOfLastInsertedHiddenUnit<maxYPosOfHiddenUnit){
  715.    SET_UNIT_XPOS(hiddenUnitPtr,*xPosOfLastInsertedHiddenUnit);
  716.    SET_UNIT_YPOS(hiddenUnitPtr,++*yPosOfLastInsertedHiddenUnit);
  717.  }
  718.  else {
  719.    SET_UNIT_XPOS(hiddenUnitPtr,++*xPosOfLastInsertedHiddenUnit);
  720.    SET_UNIT_YPOS(hiddenUnitPtr,*yPosOfLastInsertedHiddenUnit=Y_MIN_POS);
  721.    FOR_ALL_OUTPUT_UNITS(outputUnitPtr,dummy) {
  722.      SET_UNIT_XPOS(outputUnitPtr,GET_UNIT_XPOS(outputUnitPtr)+1);
  723.    }
  724.    FOR_ALL_SPECIAL_UNITS(specialUnitPtr,dummy) {
  725.      SET_UNIT_XPOS(specialUnitPtr,GET_UNIT_XPOS(specialUnitPtr)+1);
  726.    }
  727.  }
  728.  return(KRERR_NO_ERROR);
  729. }
  730.  
  731. /*****************************************************************************
  732.   FUNCTION : cc_setPointers
  733.  
  734.   PURPOSE  : Calculates the beginning of the input, hidden, output and special
  735.              units in the topo_ptr_array. 
  736.   NOTES    :
  737.  
  738.   UPDATE   : 5.2.93
  739. ******************************************************************************/
  740.  
  741.  
  742. krui_err cc_setPointers(void)
  743. {
  744.  FirstInputUnitPtr   = (struct Unit **)(&topo_ptr_array[1]);
  745.  if(*(FirstInputUnitPtr-1)!=NULL) CC_ERROR(KRERR_CC_ERROR1);
  746.  FirstHiddenUnitPtr  = FirstInputUnitPtr  + NoOfInputUnits  + 1;
  747.  if(*(FirstHiddenUnitPtr-1)!=NULL) CC_ERROR(KRERR_CC_ERROR1);
  748.  FirstOutputUnitPtr  = FirstHiddenUnitPtr + NoOfHiddenUnits + 1;
  749.  if(*(FirstOutputUnitPtr-1)!=NULL) CC_ERROR(KRERR_CC_ERROR1);
  750.  FirstSpecialUnitPtr = FirstOutputUnitPtr + NoOfOutputUnits + 1;    
  751.  if(*(FirstSpecialUnitPtr-1)!=NULL) CC_ERROR(KRERR_CC_ERROR1);
  752.  return(KRERR_NO_ERROR);
  753. }
  754.  
  755. /*****************************************************************************
  756.   FUNCTION : cc_initSpecialUnitLinks
  757.  
  758.   PURPOSE  : Sets all the links of the special units to zero.
  759.   NOTES    :
  760.  
  761.   UPDATE   : 5.2.93
  762. ******************************************************************************/
  763.  
  764.  
  765. krui_err cc_initSpecialUnitLinks(void)
  766. {
  767.  int s;
  768.  struct Unit *SpecialUnitPtr;
  769.  struct Link *LinkPtr;
  770.  
  771.  FOR_ALL_SPECIAL_UNITS(SpecialUnitPtr,s) {
  772.    SpecialUnitPtr->bias = 0.0;
  773.    BIAS_CURRENT_SLOPE(SpecialUnitPtr) = 0.0; 
  774.    BIAS_PREVIOUS_SLOPE(SpecialUnitPtr) = 0.0; 
  775.    BIAS_LAST_WEIGHT_CHANGE(SpecialUnitPtr) = 0.0;
  776.    FOR_ALL_LINKS(SpecialUnitPtr,LinkPtr) {
  777.      LinkPtr->weight = cc_generateRandomNo(CC_MAX_VALUE);
  778.      LN_CURRENT_SLOPE(LinkPtr) = 0.0;
  779.      LN_PREVIOUS_SLOPE(LinkPtr) = 0.0;
  780.      LN_LAST_WEIGHT_CHANGE(LinkPtr) = 0.0;
  781.    }
  782.  }
  783.  return(KRERR_NO_ERROR);
  784. }
  785.  
  786. /*****************************************************************************
  787.   FUNCTION : cc_updatePosOfSpecialUnits
  788.  
  789.   PURPOSE  : Updates the position of the special units.
  790.   NOTES    :
  791.  
  792.   UPDATE   : 5.2.93
  793. ******************************************************************************/
  794.  
  795. void cc_updatePosOfSpecialUnits(void)
  796. {
  797.  int x,outputXMax=0,s;
  798.  struct Unit *unitPtr,*specialUnitPtr;
  799.  
  800.  if(cc_cascade) {
  801.    FOR_ALL_UNITS(unitPtr) {
  802.      if(IS_OUTPUT_UNIT(unitPtr) && UNIT_IN_USE(unitPtr)){
  803.        x = GET_UNIT_XPOS(unitPtr);
  804.        if((x >= outputXMax) || (outputXMax==0)){
  805.          outputXMax = x;
  806.        }
  807.      }
  808.    }
  809.  
  810.    FOR_ALL_SPECIAL_UNITS(specialUnitPtr,s){
  811.      if(specialUnitPtr != NULL) { /* this can happen if there is an error before the function "cc_setPointers" */
  812.        SET_UNIT_XPOS(specialUnitPtr,outputXMax+3);
  813.      }
  814.    }
  815.    cc_cascade = 0;
  816.  }
  817. }
  818.  
  819. /*****************************************************************************
  820.   FUNCTION : cc_deleteAllSpecialUnits
  821.  
  822.   PURPOSE  : Deletes all special units.
  823.   NOTES    :
  824.  
  825.   UPDATE   : 5.2.93
  826. ******************************************************************************/
  827.  
  828. krui_err cc_deleteAllSpecialUnits(void)
  829. {
  830.  struct Unit *UnitPtr;
  831.  
  832.  if(NoOfUnits != 0) {
  833.    FOR_ALL_UNITS(UnitPtr){
  834.      if(IS_SPECIAL_UNIT(UnitPtr) && UNIT_IN_USE(UnitPtr)){
  835.        KernelErrorCode = kr_removeUnit(UnitPtr); 
  836.        ERROR_CHECK;
  837.      }
  838.    }
  839.    kr_forceUnitGC();
  840.    NetModified = 1;
  841.  }
  842.  return(KRERR_NO_ERROR);
  843. }
  844.  
  845.  
  846.  
  847. /* Begin of print functions, only needed for debugging. */
  848.  
  849. /*****************************************************************************
  850.   FUNCTION : cc_printTopoPtrArray
  851.  
  852.   PURPOSE  : Prints the topo_ptr_array.
  853.   NOTES    : Only needed for debugging.
  854.  
  855.   UPDATE   : 5.2.93
  856. ******************************************************************************/
  857.  
  858. void cc_printTopoPtrArray(void)
  859. {
  860.  int i;
  861.  printf("topo_ptr_array\n\n"); 
  862.   if( topo_ptr_array != NULL) {
  863.     for(i=0;i<NoOfUnits+5;i++) {
  864.       printf("topo_ptr_array[%d]: %d adr: %d \n",i,(int)topo_ptr_array[i],(int)&topo_ptr_array[i]);
  865.     }
  866.   }
  867. }
  868.  
  869. /*****************************************************************************
  870.   FUNCTION : cc_printUnitArray
  871.  
  872.   PURPOSE  : Prints the unit_array.
  873.   NOTES    : Only needed for debugging.
  874.  
  875.   UPDATE   : 5.2.93
  876. ******************************************************************************/
  877.  
  878. void cc_printUnitArray(void)
  879. {
  880.  struct Unit *UnitPtr;
  881.  
  882.  printf("unit_array\n\n");
  883.  
  884.  FOR_ALL_UNITS(UnitPtr) {
  885.    if(IS_INPUT_UNIT(UnitPtr)) {
  886.      printf("adr(UnitPtr): %d %s x: %d y: %d\n",(int)UnitPtr,"INPUT",
  887.             UnitPtr->unit_pos.x,UnitPtr->unit_pos.y);
  888.    }
  889.    else if(IS_HIDDEN_UNIT(UnitPtr)) {
  890.      printf("adr(UnitPtr): %d %s x: %d y: %d\n",(int)UnitPtr,"HIDDEN",
  891.             UnitPtr->unit_pos.x,UnitPtr->unit_pos.y);
  892.    }
  893.    else if(IS_OUTPUT_UNIT(UnitPtr)) {
  894.      printf("adr(UnitPtr): %d %s x: %d y: %d\n",(int)UnitPtr,"OUTPUT",
  895.             UnitPtr->unit_pos.x,UnitPtr->unit_pos.y);
  896.    }
  897.    else if(IS_SPECIAL_UNIT(UnitPtr)) {
  898.      printf("adr(UnitPtr): %d %s x: %d y: %d\n",(int)UnitPtr,"SPECIAL",
  899.             UnitPtr->unit_pos.x,UnitPtr->unit_pos.y);
  900.    }
  901.    else if(!UNIT_IN_USE(UnitPtr)) {
  902.      printf("adr(UnitPtr): %d %s\n",(int)UnitPtr,"UNIT_NOT_IN_USE");
  903.    }
  904.    else {
  905.      printf("adr(UnitPtr): %d %s\n",(int)UnitPtr,"FEHLER");
  906.    }
  907.  }
  908. }
  909.  
  910.  
  911. /*****************************************************************************
  912.   FUNCTION : cc_printUnit
  913.  
  914.   PURPOSE  : Prints information of a single Unit.
  915.   NOTES    : Only for debugging.
  916.  
  917.   UPDATE   : 5.2.93
  918. ******************************************************************************/
  919.  
  920. void cc_printUnit(struct Unit *unit_ptr)
  921. {
  922.  if(IS_INPUT_UNIT(unit_ptr)) {
  923.    printf("adr(unit_ptr): %d %s x: %d y: %d\n",(int)unit_ptr,"INPUT",
  924.    unit_ptr->unit_pos.x,unit_ptr->unit_pos.y);
  925.  }
  926.  else if(IS_HIDDEN_UNIT(unit_ptr)) {
  927.    printf("adr(unit_ptr): %d %s x: %d y: %d\n",(int)unit_ptr,"HIDDEN",
  928.    unit_ptr->unit_pos.x,unit_ptr->unit_pos.y);
  929.  }
  930.  else if(IS_OUTPUT_UNIT(unit_ptr)) {
  931.    printf("adr(unit_ptr): %d %s x: %d y: %d\n",(int)unit_ptr,"OUTPUT",
  932.    unit_ptr->unit_pos.x,unit_ptr->unit_pos.y);
  933.  }
  934.  else if(IS_SPECIAL_UNIT(unit_ptr)) {
  935.    printf("adr(unit_ptr): %d %s x: %d y: %d\n",(int)unit_ptr,"SPECIAL",
  936.    unit_ptr->unit_pos.x,unit_ptr->unit_pos.y);
  937.  }
  938.  else if(!UNIT_IN_USE(unit_ptr)) {
  939.    printf("adr(unit_ptr): %d %s\n",(int)unit_ptr,"UNIT_NOT_IN_USE");
  940.  } 
  941. }
  942.  
  943. /*****************************************************************************
  944.   FUNCTION : cc_printAllLinks
  945.  
  946.   PURPOSE  : Prints all the links of unit *unitPtr.
  947.   NOTES    : Only for debugging.
  948.  
  949.   UPDATE   : 5.2.93
  950. ******************************************************************************/
  951.  
  952. void cc_printAllLinks(struct Unit *unitPtr)
  953. {
  954.  struct Link *linkPtr;
  955.  
  956.  if(unitPtr != NULL) {
  957.    FOR_ALL_LINKS(unitPtr,linkPtr){
  958.      if(linkPtr->to != NULL){
  959.        printf("%d <-- %d %f\n",GET_UNIT_NO(unitPtr),GET_UNIT_NO(linkPtr->to),linkPtr->weight);
  960.      }
  961.      else {
  962.        printf("%d <-- ZERO \n",GET_UNIT_NO(unitPtr));
  963.      }
  964.    }
  965.  }
  966.  else {
  967.    printf("Unit has value zero\n");
  968.  }
  969.  printf("\n");
  970. }
  971.  
  972.  
  973. /* Print-Funktionen-End */
  974.  
  975.  
  976.